<?php

class EmailTaskAction extends CommonAction {

    

    public function _initialize() {

        parent::_initialize();
    }
    
    public function index() {
        $map = array();
        import("@.ORG.Util.Page");       //导入分页类
        $model = M('Email_task');

        $count = $model->count();    //计算总数

        $p = new Page($count, 10);
        $rows = $model
                ->join('email_template on email_template.template_id = email_task.template_id')
                ->field('email_task.*,email_template.title as template_title')
                ->limit($p->firstRow . ',' . $p->listRows)
                ->order('task_id desc')
                ->select();
        $page = $p->show();            //分页的导航条的输出变量
        $this->assign("page", $page);
        $this->assign("rows", $rows); //数据循环变量

        $this->display('index');
    }
    
    public function _before_add() {
        $templateModel = D('EmailTemplate');
        $this->assign('templates', $templateModel->getTemplates());
    } 
    public function _after_insert($lastInsertId) {
        $taskModel = M('Email_task');
        $task = $taskModel->where("task_id = '$lastInsertId' and status = 1")->find();
        $listModel = D('EmailTaskList');
        if(!$listModel->insertTaskList($task)) {
            $listModel->where("task_id = '$lastInsertId'")->delete();
            $taskModel->where("task_id = '$lastInsertId'")->delete();
            $this->error(L('common_insert_failed'));
        }
    }
    
    public function _after_update() {
        $lastInsertId = $this->_post('task_id');

        $taskModel = M('Email_task');
        $task = $taskModel->where("task_id = '$lastInsertId' and status = 1")->find();
        $listModel = D('EmailTaskList');
        if(!$listModel->insertTaskList($task,true)) {
            $this->error(L('common_update_failed'));
        }
    }
    public function _before_edit() {
        $templateModel = D('EmailTemplate');
        $this->assign('templates', $templateModel->getTemplates());
    } 
    public function _after_delete($ids) {
        $lastInsertId = $this->_request('ids');
        $listModel = M('Email_task_list');
        $listModel->where("task_id = '$lastInsertId'")->delete();
    }

}